from http import HTTPStatus
from urllib.parse import urlparse, unquote
from pathlib import PurePosixPath
import requests
from dashscope import ImageSynthesis
from core.config import settings
import os
from datetime import datetime
class ImageService:
    def __init__(self):
        self.api_key = settings.TONGYI_API_KEY

        ImageSynthesis.api_key = self.api_key
        self.model = "wanx2.1-t2i-turbo"
        self.result_format = "url"


    async def generate_image(self, description: str):

        
        rsp = ImageSynthesis.call(api_key=self.api_key,
                                model= "wanx2.1-t2i-turbo", 
                                prompt=description,
                                n=1,
                                size='1024*1024')
        print('response: %s' % rsp)
        if rsp.status_code == HTTPStatus.OK:
            imageUrl = rsp.output.results[0].url
            createdAt =  datetime.now()
            return {
                "imageUrl": imageUrl,
                "createdAt": createdAt,
            }
        else:
            print('sync_call Failed, status_code: %s, code: %s, message: %s' %
                (rsp.status_code, rsp.code, rsp.message))